<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form class="-mb-15px" :model="queryParams" ref="queryFormRef" :inline="true" label-width="68px">
      <el-form-item label="类型" prop="infoType">
        <el-select v-model="queryParams.infoType" placeholder="请选择类型" clearable class="!w-240px">
          <el-option v-for="dict in getIntDictOptions(DICT_TYPE.WINE_MARKET)" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="信息名称" prop="infoNme">
        <el-input v-model="queryParams.infoNme" placeholder="请输入信息名称" clearable @keyup.enter="handleQuery"
          class="!w-240px" />
      </el-form-item>
      <el-form-item label="价格状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择价格状态" clearable class="!w-240px">
          <el-option v-for="dict in getIntDictOptions(DICT_TYPE.PRICE_STATE)" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery">
          <Icon icon="ep:search" class="mr-5px" /> 搜索
        </el-button>
        <el-button @click="resetQuery">
          <Icon icon="ep:refresh" class="mr-5px" /> 重置
        </el-button>
        <el-button type="primary" plain @click="openForm(undefined)" v-hasPermi="['wine:supply-market:create']">
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <!-- <el-button type="success" plain @click="handleExport" :loading="exportLoading"
          v-hasPermi="['wine:supply-market:export']">
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button> -->
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="类型" align="center" prop="infoType">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.WINE_MARKET" :value="scope.row.infoType" />
        </template>
      </el-table-column>
      <el-table-column label="简短标题" align="center" prop="infoTitle" />
      <el-table-column label="香型" align="center" prop="fragranceType">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.ODOR_TYPE" :value="scope.row.fragranceType" />
        </template>
      </el-table-column>

      <el-table-column label="年份" align="center" prop="showYear">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.YEAR" :value="scope.row.showYear" />
        </template>
      </el-table-column>
      <el-table-column label="信息名称" align="center" prop="infoNme" />
      <el-table-column label="包装" align="center" prop="packing">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.PACKING" :value="scope.row.packing" />
        </template>
      </el-table-column>

      <el-table-column label="均价" align="center" prop="averagePrice" />
      <el-table-column label="涨幅量" align="center" prop="salePrice" />
      <el-table-column label="价格状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.PRICE_STATE" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" :formatter="dateFormatter" width="180px" />
      <el-table-column label="操作" align="center" width="100px">
        <template #default="scope">
          <!-- <el-button link type="primary" @click="handleAudit(scope.row.id)" v-hasPermi="['wine:supply-market:query']">
            详情
          </el-button>
          <el-button link type="primary" @click="openForm(scope.row.id)" v-hasPermi="['wine:supply-market:update']">
            编辑
          </el-button>
          -->
          <el-button link type="danger" @click="handleDelete(scope.row.id)" v-hasPermi="['wine:supply-market:delete']">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination :total="total" v-model:page="queryParams.pageNo" v-model:limit="queryParams.pageSize"
      @pagination="getList" />
  </ContentWrap>

</template>

<script setup lang="ts">
import { dateFormatter } from '@/utils/formatTime'
// import download from '@/utils/download'
import { SupplyMarketApi, SupplyMarketVO } from '@/api/wine/supplymarket'
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
const { push } = useRouter() // 路由跳转
/** 酒类行情管理 列表 */
defineOptions({ name: 'SupplyMarket' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<SupplyMarketVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  infoType: undefined,
  infoNme: undefined,
  infoTitle: undefined,
  showYear: undefined,
  fragranceType: undefined,
  averagePrice: undefined,
  salePrice: undefined,
  description: undefined,
  status: undefined,
  sort: undefined,
  isTop: undefined,
  countTime: [],
  createTime: []
})
const queryFormRef = ref() // 搜索的表单
// const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await SupplyMarketApi.getSupplyMarketPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

const handleAudit = (id: number) => {
  push({ name: 'SupplyMarketDetail', params: { id } })
}


/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (id?: number) => {
  // 修改
  if (typeof id === 'number') {
    push({ name: 'SupplyMarketEdit', params: { id } })
    return
  }
  // 新增
  push({ name: 'SupplyMarketAdd' })
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await SupplyMarketApi.deleteSupplyMarket(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch { }
}

/** 导出按钮操作 */
// const handleExport = async () => {
//   try {
//     // 导出的二次确认
//     await message.exportConfirm()
//     // 发起导出
//     exportLoading.value = true
//     const data = await SupplyMarketApi.exportSupplyMarket(queryParams)
//     download.excel(data, '酒类行情管理.xls')
//   } catch {
//   } finally {
//     exportLoading.value = false
//   }
// }

/** 初始化 **/
onActivated(async () => {
  await getList()
})

/** 初始化 **/
onMounted(async () => {
  await getList()
})
</script>